﻿@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Forms
@using Covenant.Models.Covenant
@inject IJSRuntime IJSRuntime

<EditForm Model="Credential" OnValidSubmit="OnFormSubmit">
    <DataAnnotationsValidator />
    <div class="form-row">
        <div class="form-group">
            <label for="Type">Type</label><br />
            <select id="Type" name="Type" value="@Credential.Type" @onchange="(e => OnChangeType(e))" class="selectpicker show-menu-arrow" data-dropup-auto="false" data-width="auto">
                @foreach (var n in Enum.GetNames(typeof(CredentialType)))
                {
                    if (Enum.TryParse<CredentialType>(n, out CredentialType type) && this.Credential.Type == type)
                    {
                        <option selected value="@(((Enum)typeof(CredentialType).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                    }
                    else
                    {
                        <option value="@(((Enum)typeof(CredentialType).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                    }
                }
            </select>
            <div class="text-danger"><ValidationMessage For="() => Credential.Type" /></div>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="Domain">Domain</label>
            <input id="Domain" name="Domain" @bind="Credential.Domain" class="form-control">
            <div class="text-danger"><ValidationMessage For="() => Credential.Domain" /></div>
        </div>
        <div class="form-group col-md-6">
            <label for="Username">Username</label>
            <input id="Username" name="Username" @bind="Credential.Username" class="form-control">
            <div class="text-danger"><ValidationMessage For="() => Credential.Username" /></div>
        </div>
    </div>
    @if (Credential.Type == CredentialType.Password)
    {
        <div class="form-row">
            <div class="form-group col-md-6">
                <label for="Password">Password</label>
                <input id="Password" name="Password" @bind="((CapturedPasswordCredential)Credential).Password" class="form-control">
                <div class="text-danger"><ValidationMessage For="() => ((CapturedPasswordCredential)Credential).Password" /></div>
            </div>
        </div>
    }
    @if (Credential.Type == CredentialType.Hash)
    {
        <div class="form-row">
            <div class="form-group col-md-3">
                <label for="HashCredentialType">HashCredentialType</label><br />
                <select id="HashCredentialType" name="HashCredentialType" @bind="((CapturedHashCredential)Credential).HashCredentialType" class="selectpicker show-menu-arrow" data-dropup-auto="false" data-width="auto">
                    @foreach (var n in Enum.GetNames(typeof(HashType)))
                    {
                        if (Enum.TryParse<HashType>(n, out HashType type) && ((CapturedHashCredential)Credential).HashCredentialType == type)
                        {
                            <option selected value="@(((Enum)typeof(HashType).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                        }
                        else
                        {
                            <option value="@(((Enum)typeof(HashType).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                        }
                    }
                </select>
                <div class="text-danger"><ValidationMessage For="() => ((CapturedHashCredential)Credential).HashCredentialType" /></div>
            </div>
            <div class="form-group col-md-9">
                <label for="Hash">Hash</label>
                <input id="Hash" name="Hash" @bind="((CapturedHashCredential)Credential).Hash" class="form-control">
                <div class="text-danger"><ValidationMessage For="() => ((CapturedHashCredential)Credential).Hash" /></div>
            </div>
        </div>
    }
    @if (Credential.Type == CredentialType.Ticket)
    {
        <div class="form-row">
            <div class="form-group col-md-3">
                <label for="TicketCredentialType">TicketCredentialType</label><br />
                <select id="TicketCredentialType" name="TicketCredentialType" @bind="((CapturedTicketCredential)Credential).TicketCredentialType" class="selectpicker show-menu-arrow" data-dropup-auto="false" data-width="auto">
                    @foreach (var n in Enum.GetNames(typeof(TicketType)))
                    {
                        if (Enum.TryParse<TicketType>(n, out TicketType type) && ((CapturedTicketCredential)Credential).TicketCredentialType == type)
                        {
                            <option selected value="@(((Enum)typeof(TicketType).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                        }
                        else
                        {
                            <option value="@(((Enum)typeof(TicketType).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                        }
                    }
                </select>
                <div class="text-danger"><ValidationMessage For="() => ((CapturedTicketCredential)Credential).TicketCredentialType" /></div>
            </div>
            <div class="form-group col-md-9">
                <label for="ServiceName">ServiceName</label>
                <input id="ServiceName" name="ServiceName" @bind="((CapturedTicketCredential)Credential).ServiceName" class="form-control">
                <div class="text-danger"><ValidationMessage For="() => ((CapturedTicketCredential)Credential).ServiceName" /></div>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-6">
                <label for="Ticket">Ticket</label>
                <input id="Ticket" name="Ticket" @bind="((CapturedTicketCredential)Credential).Ticket" class="form-control">
                <div class="text-danger"><ValidationMessage For="() => ((CapturedTicketCredential)Credential).Ticket" /></div>
            </div>
        </div>
    }
    <button type="button" @onclick="(e => OnSubmit.InvokeAsync(Credential))" class="btn btn-primary">
        <span class="fe fe-@SubmitIcon"></span> @SubmitLabel
    </button>
</EditForm>

@code {
    [Parameter]
    public CapturedCredential Credential { get; set; }

    [Parameter]
    public EventCallback<CapturedCredential> OnSubmit { get; set; }

    [Parameter]
    public string SubmitIcon { get; set; }

    [Parameter]
    public string SubmitLabel { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await IJSRuntime.InvokeAsync<string>("InitializeSelectPicker", "#Type.selectpicker", ((int)Enum.Parse(typeof(CredentialType), this.Credential.Type.ToString())).ToString());
        if (this.Credential.Type == CredentialType.Hash)
        {
            await IJSRuntime.InvokeAsync<string>("InitializeSelectPicker", "#HashCredentialType.selectpicker", ((int)Enum.Parse(typeof(HashType), ((CapturedHashCredential)this.Credential).HashCredentialType.ToString())).ToString());
        }
        if (this.Credential.Type == CredentialType.Ticket)
        {
            await IJSRuntime.InvokeAsync<string>("InitializeSelectPicker", "#TicketCredentialType.selectpicker", ((int)Enum.Parse(typeof(TicketType), ((CapturedTicketCredential)this.Credential).TicketCredentialType.ToString())).ToString());
        }
    }

    private void OnChangeType(ChangeEventArgs e)
    {
        CredentialType type = Enum.Parse<CredentialType>((string)e.Value);
        switch (type)
        {
            case CredentialType.Password:
                this.Credential = new CapturedPasswordCredential
                {
                    Id = this.Credential.Id,
                    Type = type,
                    Domain = this.Credential.Domain,
                    Username = this.Credential.Username
                };
                break;
            case CredentialType.Hash:
                this.Credential = new CapturedHashCredential
                {
                    Id = this.Credential.Id,
                    Type = type,
                    Domain = this.Credential.Domain,
                    Username = this.Credential.Username
                };
                break;
            case CredentialType.Ticket:
                this.Credential = new CapturedTicketCredential
                {
                    Id = this.Credential.Id,
                    Type = type,
                    Domain = this.Credential.Domain,
                    Username = this.Credential.Username
                };
                break;
        }
    }

    private async Task OnFormSubmit()
    {
        await this.OnSubmit.InvokeAsync(this.Credential);
    }
}
